using System.Collections;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;

public class LightworldMenu : MonoBehaviour
{
	public PlayerManager PlayerManager;

	[Header("UI")]
	[Space(15f)]
	public CHATBOXTEXT[] QueuedCharacterCalls;

	public CHATBOXTEXT NoResponseCall;

	public INT_Chat PlayerCallChat;

	[Space(15f)]
	public Vector3[] ItemMenuOptions;

	public Vector3[] ItemMenuSelectedOptions;

	public TextMeshProUGUI[] ItemInventoryText;

	private int currentItemMenuState;

	private int currentSelectedItem;

	private int InventoryLength;

	private int LastItemSelectedIndex;

	[Space(15f)]
	public bool IsInLightworld = true;

	public GameObject MenuHolder;

	public RectTransform[] BaseMenuOptions;

	[SerializeField]
	private PlayerManager.PlayerState PreviousPlayerState;

	public Transform[] CallMenuOptions;

	public int[] CallChatIndexes;

	public string[] CallMenuNames;

	[Space(15f)]
	public GameObject LightworldStats;

	public GameObject LightworldCall;

	public GameObject LightworldItem;

	public bool CanOpenMenu;

	public bool MenuOpen;

	public bool AllowInput = true;

	private bool MenuToggleCooldown;

	public string CurrentOpenMenu;

	public RectTransform HeartCursor;

	private int CursorPos;

	public AudioSource MenuSound;

	private bool ChatboxRunning;

	public AudioClip CantSelectSound;

	public AudioClip SelectSound;

	public AudioClip ClickSound;

	public AudioClip RingSound;

	private InventoryItem CurrentHoveredItem;

	private static LightworldMenu instance;

	public static LightworldMenu Instance => instance;

	private void Awake()
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
			return;
		}
		instance = this;
		Object.DontDestroyOnLoad((Object)(object)instance);
	}

	private void Start()
	{
		PlayerManager = Object.FindFirstObjectByType<PlayerManager>();
		if ((Object)(object)PlayerManager != (Object)null)
		{
			PlayerCallChat = PlayerManager.PlayerINT_Chat;
			MenuSound = ((Component)PlayerManager).GetComponent<AudioSource>();
		}
	}

	private void Update()
	{
		if ((Object)(object)PlayerManager == (Object)null)
		{
			PlayerManager = Object.FindFirstObjectByType<PlayerManager>();
			PlayerCallChat = PlayerManager.PlayerINT_Chat;
			MenuSound = ((Component)PlayerManager).GetComponent<AudioSource>();
		}
		if (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			ChatboxRunning = true;
		}
		else if (ChatboxRunning)
		{
			((MonoBehaviour)this).StartCoroutine(ChatboxToLightworldMenuDebounce());
		}
		else
		{
			ChatboxRunning = false;
		}
		if (!ChatboxRunning)
		{
			ProcessInputs();
		}
		if (MenuOpen)
		{
			if ((Object)(object)PlayerManager != (Object)null && PlayerManager._PlayerState == PlayerManager.PlayerState.Game)
			{
				ForceCloseMenu();
			}
			else
			{
				LightworldMenuUpdate();
			}
		}
	}

	private void ProcessInputs()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Menu) && !MenuToggleCooldown && PlayerManager._PlayerState != PlayerManager.PlayerState.Cutscene && !ChatboxManager.Instance.ChatIsCurrentlyRunning && CurrentOpenMenu == "" && IsInLightworld && AllowInput && CanOpenMenu)
		{
			MenuOpen = !MenuOpen;
			MenuHolder.SetActive(MenuOpen);
			if (MenuOpen)
			{
				PreviousPlayerState = PlayerManager._PlayerState;
				PlayerManager._PlayerState = PlayerManager.PlayerState.NoPlayerMovement;
			}
			else
			{
				PlayerManager._PlayerState = PreviousPlayerState;
			}
			MenuToggleCooldown = true;
			((MonoBehaviour)this).StartCoroutine(ToggleMenuCooldown());
		}
	}

	private IEnumerator ToggleMenuCooldown()
	{
		yield return (object)new WaitForSeconds(0.1f);
		MenuToggleCooldown = false;
	}

	private IEnumerator ChatboxToLightworldMenuDebounce()
	{
		yield return (object)new WaitForSeconds(0.2f);
		ChatboxRunning = false;
	}

	private void LightworldMenuUpdate()
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0259: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_031f: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_077e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0368: Unknown result type (might be due to invalid IL or missing references)
		//IL_07c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0216: Unknown result type (might be due to invalid IL or missing references)
		//IL_0599: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_083c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0859: Unknown result type (might be due to invalid IL or missing references)
		//IL_0869: Unknown result type (might be due to invalid IL or missing references)
		//IL_086e: Unknown result type (might be due to invalid IL or missing references)
		//IL_08a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0714: Unknown result type (might be due to invalid IL or missing references)
		//IL_050b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0472: Unknown result type (might be due to invalid IL or missing references)
		//IL_095a: Unknown result type (might be due to invalid IL or missing references)
		//IL_057e: Unknown result type (might be due to invalid IL or missing references)
		switch (CurrentOpenMenu)
		{
		case "call":
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Up) && CurrentOpenMenu != "stat" && AllowInput)
			{
				MenuSound.PlayOneShot(SelectSound);
				CursorPos--;
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Down) && CurrentOpenMenu != "stat" && AllowInput)
			{
				MenuSound.PlayOneShot(SelectSound);
				CursorPos++;
			}
			SetupCallNames();
			int num = 0;
			string[] callMenuNames = CallMenuNames;
			for (int i = 0; i < callMenuNames.Length; i++)
			{
				if (callMenuNames[i] != "")
				{
					num++;
				}
			}
			CursorPos = Mathf.Clamp(CursorPos, 0, num - 1);
			((Transform)HeartCursor).position = CallMenuOptions[CursorPos].position + new Vector3(-55f, -3f, 0f);
			LightworldCall.SetActive(true);
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && AllowInput)
			{
				if ((Object)(object)QueuedCharacterCalls[CursorPos] != (Object)null && CallChatIndexes[CursorPos] < QueuedCharacterCalls[CursorPos].Textboxes.Length)
				{
					PlayerCallChat.Text = QueuedCharacterCalls[CursorPos];
					PlayerCallChat.CurrentIndex = CallChatIndexes[CursorPos];
				}
				else
				{
					PlayerCallChat.Text = NoResponseCall;
					PlayerCallChat.CurrentIndex = 0;
				}
				InitiateCall();
				MenuSound.PlayOneShot(ClickSound);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && PlayerManager._PlayerState != PlayerManager.PlayerState.Cutscene && AllowInput)
			{
				CursorPos = 2;
				CurrentOpenMenu = "";
			}
			return;
		}
		case "stat":
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Up) && CurrentOpenMenu != "stat" && AllowInput)
			{
				MenuSound.PlayOneShot(SelectSound);
				CursorPos--;
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Down) && CurrentOpenMenu != "stat" && AllowInput)
			{
				MenuSound.PlayOneShot(SelectSound);
				CursorPos++;
			}
			((Transform)HeartCursor).localPosition = Vector2.op_Implicit(new Vector2(999f, 999f));
			LightworldStats.SetActive(true);
			CursorPos = 1;
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && AllowInput)
			{
				CursorPos = 1;
				CurrentOpenMenu = "";
			}
			return;
		case "item":
			if (InventoryLength > 0)
			{
				if (currentItemMenuState == 0)
				{
					if (Input.GetKeyDown(PlayerInput.Instance.Key_Up) && CurrentOpenMenu != "stat" && AllowInput)
					{
						MenuSound.PlayOneShot(SelectSound);
						CursorPos--;
						CursorPos = Mathf.Clamp(CursorPos, 0, InventoryLength - 1);
					}
					if (Input.GetKeyDown(PlayerInput.Instance.Key_Down) && CurrentOpenMenu != "stat" && AllowInput)
					{
						MenuSound.PlayOneShot(SelectSound);
						CursorPos++;
						CursorPos = Mathf.Clamp(CursorPos, 0, InventoryLength - 1);
					}
					CurrentHoveredItem = LightworldInventory.Instance.PlayerInventory[CursorPos];
					CursorPos = Mathf.Clamp(CursorPos, 0, InventoryLength - 1);
					((Transform)HeartCursor).localPosition = ItemMenuOptions[CursorPos];
					LastItemSelectedIndex = CursorPos;
				}
			}
			else
			{
				CursorPos = 1;
				((Transform)HeartCursor).localPosition = Vector2.op_Implicit(new Vector2(-500f, -500f));
			}
			if (currentItemMenuState == 1)
			{
				if (Input.GetKeyDown(PlayerInput.Instance.Key_Left) && CurrentOpenMenu != "stat" && AllowInput)
				{
					MenuSound.PlayOneShot(SelectSound);
					CursorPos--;
				}
				if (Input.GetKeyDown(PlayerInput.Instance.Key_Right) && CurrentOpenMenu != "stat" && AllowInput)
				{
					MenuSound.PlayOneShot(SelectSound);
					CursorPos++;
				}
				CursorPos = Mathf.Clamp(CursorPos, 0, ItemMenuSelectedOptions.Length - 1);
				((Transform)HeartCursor).localPosition = ItemMenuSelectedOptions[CursorPos];
			}
			LightworldItem.SetActive(true);
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && AllowInput)
			{
				if (currentItemMenuState == 1)
				{
					ResetCallChat();
					if (CursorPos == 0)
					{
						if ((Object)(object)CurrentHoveredItem != (Object)null && (Object)(object)CurrentHoveredItem.UseText != (Object)null)
						{
							PlayerCallChat.Text = CurrentHoveredItem.UseText;
							StartChat(0);
						}
						else
						{
							MenuSound.PlayOneShot(CantSelectSound);
						}
					}
					else if (CursorPos == 1)
					{
						if ((Object)(object)CurrentHoveredItem != (Object)null && (Object)(object)CurrentHoveredItem.InfoText != (Object)null)
						{
							PlayerCallChat.Text = CurrentHoveredItem.InfoText;
							StartChat(0);
						}
						else
						{
							MenuSound.PlayOneShot(CantSelectSound);
						}
					}
					else if ((Object)(object)CurrentHoveredItem != (Object)null && (Object)(object)CurrentHoveredItem.DropText != (Object)null)
					{
						PlayerCallChat.Text = CurrentHoveredItem.DropText;
						StartChat(0);
					}
					else
					{
						MenuSound.PlayOneShot(CantSelectSound);
					}
				}
				if (currentItemMenuState == 0 && InventoryLength > 0)
				{
					currentSelectedItem = CursorPos;
					CursorPos = 0;
					currentItemMenuState = 1;
				}
				MenuSound.PlayOneShot(ClickSound);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && PlayerManager._PlayerState != PlayerManager.PlayerState.Cutscene && AllowInput)
			{
				if (currentItemMenuState == 0)
				{
					CursorPos = 0;
					CurrentOpenMenu = "";
				}
				if (currentItemMenuState == 1)
				{
					CursorPos = currentSelectedItem;
					currentItemMenuState = 0;
				}
			}
			return;
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Up) && CurrentOpenMenu != "stat" && AllowInput)
		{
			MenuSound.PlayOneShot(SelectSound);
			CursorPos--;
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Down) && CurrentOpenMenu != "stat" && AllowInput)
		{
			MenuSound.PlayOneShot(SelectSound);
			CursorPos++;
		}
		CursorPos = Mathf.Clamp(CursorPos, 0, BaseMenuOptions.Length - 1);
		((Transform)HeartCursor).localPosition = Vector2.op_Implicit(new Vector2(BaseMenuOptions[CursorPos].anchoredPosition.x - 59f, BaseMenuOptions[CursorPos].anchoredPosition.y - 17f));
		LightworldCall.SetActive(false);
		LightworldStats.SetActive(false);
		LightworldItem.SetActive(false);
		currentItemMenuState = 0;
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && AllowInput)
		{
			if (CursorPos == 0)
			{
				SetupInventoryText();
				MenuSound.PlayOneShot(ClickSound);
				CurrentOpenMenu = "item";
				CursorPos = 0;
			}
			else if (CursorPos == 1)
			{
				MenuSound.PlayOneShot(ClickSound);
				CurrentOpenMenu = "stat";
				CursorPos = 1;
			}
			else if (CursorPos == 2)
			{
				MenuSound.PlayOneShot(ClickSound);
				CurrentOpenMenu = "call";
				CursorPos = 0;
				ResetCallChat();
			}
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && PlayerManager._PlayerState != PlayerManager.PlayerState.Cutscene && AllowInput)
		{
			MenuOpen = false;
			MenuHolder.SetActive(false);
			LightworldCall.SetActive(false);
			LightworldStats.SetActive(false);
			CurrentOpenMenu = "";
			PlayerManager._PlayerState = PlayerManager.PlayerState.Game;
		}
	}

	private void InitiateCall()
	{
		MenuOpen = false;
		MenuHolder.SetActive(false);
		LightworldCall.SetActive(false);
		LightworldStats.SetActive(false);
		CurrentOpenMenu = "";
		MenuSound.PlayOneShot(RingSound);
		PlayerManager._PlayerState = PlayerManager.PlayerState.Game;
		PlayerCallChat.RUN();
		CallChatIndexes[CursorPos]++;
		CursorPos = 0;
	}

	public void ForceCloseMenu()
	{
		MenuOpen = false;
		MenuHolder.SetActive(false);
		LightworldCall.SetActive(false);
		LightworldStats.SetActive(false);
		CurrentOpenMenu = "";
		CursorPos = 0;
	}

	public void StartChat(int index)
	{
		MenuOpen = false;
		CursorPos = 0;
		PlayerCallChat.FirstTextPlayed = false;
		MenuHolder.SetActive(false);
		LightworldCall.SetActive(false);
		LightworldStats.SetActive(false);
		PlayerCallChat.CurrentIndex = index;
		CurrentOpenMenu = "";
		PlayerManager._PlayerState = PlayerManager.PlayerState.Game;
		PlayerCallChat.RUN();
	}

	public void DropLastSelectedItem()
	{
		if (LightworldInventory.Instance.PlayerInventory.Count > 0 && (Object)(object)LightworldInventory.Instance.PlayerInventory[LastItemSelectedIndex] != (Object)null)
		{
			LightworldInventory.Instance.RemoveItem(LastItemSelectedIndex);
		}
	}

	private void SetupInventoryText()
	{
		InventoryLength = 0;
		for (int i = 0; i < LightworldInventory.Instance.PlayerInventory.Count; i++)
		{
			if (LightworldInventory.Instance.PlayerInventory.Count > 0 && (Object)(object)LightworldInventory.Instance.PlayerInventory[i] != (Object)null && LightworldInventory.Instance.PlayerInventory[i].ItemName != "")
			{
				((TMP_Text)ItemInventoryText[i]).text = LightworldInventory.Instance.PlayerInventory[i].ItemName;
				InventoryLength++;
				((TMP_Text)ItemInventoryText[i]).text = Regex.Replace(((TMP_Text)ItemInventoryText[i]).text, "([‘’“”\\-\\u201A-\\u201F\\u4E00-\\u9FFF\\u3000-\\u303F\\uFF00-\\uFFEF])", "$1 ");
				((TMP_Text)ItemInventoryText[i]).text = Regex.Replace(((TMP_Text)ItemInventoryText[i]).text, "([a-zA-Z0-9\\.\\-@#])([‘’“”\\u201A-\\u201F\\u4E00-\\u9FFF\\u3000-\\u303F\\uFF00-\\uFFEF])", "$1 $2");
			}
			else
			{
				((TMP_Text)ItemInventoryText[i]).text = "";
			}
		}
	}

	private void ResetCallChat()
	{
		PlayerCallChat.CanUse = true;
		PlayerCallChat.FinishedText = false;
		PlayerCallChat.FirstTextPlayed = false;
		PlayerCallChat.CurrentIndex = 0;
	}

	private void SetupCallNames()
	{
		for (int i = 0; i < CallMenuOptions.Length; i++)
		{
			if (i < QueuedCharacterCalls.Length && CallMenuNames[i] != "" && CallMenuNames[i] != null)
			{
				if (CallMenuNames[i].Contains('#'))
				{
					string text = CallMenuNames[i].Replace("#", string.Empty);
					((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text = text;
				}
				else
				{
					((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text = "给" + CallMenuNames[i] + "打电话";
				}
				((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text = Regex.Replace(((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text, "([‘’“”\\-\\u201A-\\u201F\\u4E00-\\u9FFF\\u3000-\\u303F\\uFF00-\\uFFEF])", "$1 ");
				((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text = Regex.Replace(((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text, "([a-zA-Z0-9\\.\\-@#])([‘’“”\\u201A-\\u201F\\u4E00-\\u9FFF\\u3000-\\u303F\\uFF00-\\uFFEF])", "$1 $2");
			}
			else
			{
				((TMP_Text)((Component)CallMenuOptions[i]).GetComponent<TextMeshProUGUI>()).text = "";
			}
		}
	}
}
